<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    const UPDATED_AT = null;
    const CREATED_AT = null;
    public $table = 'category';

    //
    public static function category_cache()
    {
        $category = array();
        $categoryData = Category::select('id', 'alias', 'name')->where(['pid' => 0])->orderBy('id', 'asc')->get();

        foreach ($categoryData as $key => $item) {
            $category[$item->alias][$item->id] = $item->name;
        }
        return $category;
    }
    public static function category_caches()
    {
        $category = array();
        $categoryData = Category::select('id', 'alias', 'name')->orderBy('id', 'asc')->get();

        foreach ($categoryData as $key => $item) {
            $category[$item->alias][$item->id] = $item->name;
        }
        return $category;
    }

    public static function getTag($tag)
    {
        $tagArr = explode(',', $tag);
        $categoryData = Category::select('id', 'name')->whereIn('id', $tagArr)->orderBy('id', 'desc')->get();
        return $categoryData;
    }

    /*
     * 单个
     */
    public static function getCategoryCn($category, $key, $index)
    {
        return isSet($category[$key][$index]) ? $category[$key][$index] : "";
    }

    public static function getCategoryCns($category, $key, $index)
    {
        $result = [];
        $list = $category[$key];
        $indexArr = explode(',', $index);
        foreach ($indexArr as $i) {
            if (isSet($list[$i])) {
                $result[] = $list[$i];
            }
        }
        return implode("+", $result);
    }
}
